package com.cgtong.android.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cgtong.android.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ReportPopupWindow extends PopupWindow {

    @BindView(R.id.yearly_report_txt)
    TextView mYearlyReportTxt;
    @BindView(R.id.monthly_report_txt)
    TextView mMonthlyReportTxt;
    @BindView(R.id.daily_report_txt)
    TextView mDailyReportTxt;
    private int popupWidth;
    private int popupHeight;


    // 坐标的位置（x、y）
    private final int[] location = new int[2];
    // 弹窗子类项选中时的监听
    private OnItemClickListener mItemClickListener;
    // 定义弹窗子类项列表

    public void setItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }


    public ReportPopupWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_report_popupwindow, null);
        ButterKnife.bind(this, view);

        this.setContentView(view);
        this.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.PopWindowTheme);

        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = view.getMeasuredHeight();
        popupWidth = view.getMeasuredWidth();
    }

    public void showPopupWindow(View v) {
        v.getLocationOnScreen(location);

        if (!this.isShowing()) {
//            showAsDropDown(v);
            showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1]+v.getHeight() );

        } else {
            dismiss();
        }
    }


    @OnClick({R.id.yearly_report_txt, R.id.monthly_report_txt, R.id.daily_report_txt})
    public void onViewClicked(View view) {
        dismiss();
        switch (view.getId()) {
            case R.id.yearly_report_txt:
                if (mItemClickListener != null)
                    mItemClickListener.onItemClick(2);
                break;
            case R.id.monthly_report_txt:
                if (mItemClickListener != null)
                    mItemClickListener.onItemClick(1);
                break;
            case R.id.daily_report_txt:
                if (mItemClickListener != null)
                    mItemClickListener.onItemClick(0);
                break;
        }
    }

    /**
     * 功能描述：弹窗子类项按钮监听事件
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
